package com.foxit.setting;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.SimpleDateFormat;
import java.util.SimpleTimeZone;

import com.alibaba.fastjson.JSON;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;

public class Sethome extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       request.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       try {
    	  
          MongoClient mongo = new MongoClient("192.201.108.97", 27017);
          DB db = mongo.getDB("settingdb");
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
          sdf.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
          DBCollection table = db.getCollection("setlog");
          String r = request.getParameter("del"); //yyyyMMddHHmm
          if(r != null && r.length()!=0) {
             //Date date=sdf.parse(r);
             table.remove(new BasicDBObject().append("start", r));
             // table adrule
             DBCollection table1 = db.getCollection("adrule");
             table1.remove(new BasicDBObject().append("start",r));
             // table adid
             DBCollection table2 = db.getCollection("adid");
             table2.remove(new BasicDBObject().append("start", r));
             // table top
             DBCollection table3 = db.getCollection("top");
             table3.remove(new BasicDBObject().append("start", r));
             // table  action 
             DBCollection table4 = db.getCollection("action");
             table4.remove(new BasicDBObject().append("start", r));
             // table  adana 
             DBCollection table5 = db.getCollection("adana");
             table5.remove(new BasicDBObject().append("start", r));
             // table adanarule
             DBCollection table6 = db.getCollection("adanarule");
             table6.remove(new BasicDBObject().append("start", r));
             // table default
             DBCollection table7 = db.getCollection("default");
             table7.remove(new BasicDBObject().append("start", r));
             // table groupad
             DBCollection table8 = db.getCollection("groupad");
             table8.remove(new BasicDBObject().append("start", r));
             // table grouptype
             DBCollection table9 = db.getCollection("grouptype");
             table9.remove(new BasicDBObject().append("start", r));
             // table impmsgad
             DBCollection table10 = db.getCollection("impmsgad");
             table10.remove(new BasicDBObject().append("start",r));
             // table member
             DBCollection table11 = db.getCollection("member");
             table11.remove(new BasicDBObject().append("start", r));
             // table policy
             DBCollection table12 = db.getCollection("policy");
             table12.remove(new BasicDBObject().append("start", r));
             // table round
             DBCollection table13 = db.getCollection("round");
             table13.remove(new BasicDBObject().append("start", r));
             // table server
             DBCollection table14 = db.getCollection("server");
             table14.remove(new BasicDBObject().append("start", r));

             response.sendRedirect("./Sethome");
             return;
          }
          out.println("<HTML><head><style>table,th,td { border:1px solid black;border-collapse:collapse; } </style></head><body>");
          out.println("<FORM action='./Slist'><table border='1' style='width:300px'><tr><th colspan=6>FOXIT Setting Tool</th></tr>");
        
          DBCursor cursor = table.find();
          while (cursor.hasNext()) {
             Setlog s = (Setlog) JSON.parseObject(cursor.next().toString(),Setlog.class);
             if(s.getOnline()!= 1) {
                out.println("<tr><td>"+s.getStart()+"</td><td>"+s.getStart()+"</td><td><button name='modify' value='"+s.getStart()+"'>modify</button></td><td></td><td></td><td>   <a href='./Sethome?del="+s.getStart()+"'>delete</a></button></td></tr>");
             } else {
                out.println("<tr><td>"+s.getStart()+"</td><td>"+s.getStart()+"</td><td><button name='modify' value='"+s.getStart()+"'>modify</button></td><td></td><td></td><td><font color=red>On-line</font></td></tr>");
             }
          }
          out.println("</table></FORM><br>");
          out.println("<FORM action='./SetNew'><table border=1 style='width:300px'><tr><td>");
          out.println("<select name='year'><option value='2014'>2014</option><option value='2015'>2015</option><option value='2016'>2016</option><option value='2017'>2017</option><option value='2018'>2018</option></select>YEAR<p>");
          out.println("<select name='month'><option value='01'>1</option><option value='02'>2</option><option value='03'>3</option><option value='04'>4</option><option value='05'>5</option><option value='06'>6</option><option value='07'>7</option><option value='08'>8</option><option value='09'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option></select>MONTH<p>");
          out.println("<select name='day'><option value='01'>1</option><option value='02'>2</option><option value='03'>3</option><option value='04'>4</option><option value='05'>5</option><option value='06'>6</option><option value='07'>7</option><option value='08'>8</option><option value='09'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option><option value='13'>13</option><option value='14'>14</option><option value='15'>15</option><option value='16'>16</option><option value='17'>17</option><option value='18'>18</option><option value='19'>19</option><option value='20'>20</option><option value='21'>21</option><option value='22'>22</option><option value='23'>23</option><option value='24'>24</option><option value='25'>25</option><option value='26'>26</option><option value='27'>27</option><option value='28'>28</option><option value='29'>29</option><option value='30'>30</option><option value='31'>31</option></select>DAY<p>");
          out.println("<select name='hour'><option value='00'>0</option><option value='01'>1</option><option value='02'>2</option><option value='03'>3</option><option value='04'>4</option><option value='05'>5</option><option value='06'>6</option><option value='07'>7</option><option value='08'>8</option><option value='09'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option><option value='13'>13</option><option value='14'>14</option><option value='15'>15</option><option value='16'>16</option><option value='17'>17</option><option value='18'>18</option><option value='19'>19</option><option value='20'>20</option><option value='21'>21</option><option value='22'>22</option><option value='23'>23</option></select>HOUR<p>");
          out.println("<select name='min'><option value='00'>0</option><option value='01'>1</option><option value='02'>2</option><option value='03'>3</option><option value='04'>4</option><option value='05'>5</option><option value='06'>6</option><option value='07'>7</option><option value='08'>8</option><option value='09'>9</option><option value='10'>10</option><option value='11'>11</option><option value='12'>12</option><option value='13'>13</option><option value='14'>14</option><option value='15'>15</option><option value='16'>16</option><option value='17'>17</option><option value='18'>18</option><option value='19'>19</option><option value='20'>20</option><option value='21'>21</option><option value='22'>22</option><option value='23'>23</option><option value='24'>24</option><option value='25'>25</option><option value='26'>26</option><option value='27'>27</option><option value='28'>28</option><option value='29'>29</option><option value='30'>30</option><option value='31'>31</option><option value='32'>32</option><option value='33'>33</option><option value='34'>34</option><option value='35'>35</option><option value='36'>36</option><option value='37'>37</option><option value='38'>38</option><option value='39'>39</option><option value='40'>40</option><option value='41'>41</option><option value='42'>42</option><option value='43'>43</option><option value='44'>44</option><option value='45'>45</option><option value='46'>46</option><option value='47'>47</option><option value='48'>48</option><option value='49'>49</option><option value='50'>50</option><option value='51'>51</option><option value='52'>52</option><option value='53'>53</option><option value='54'>54</option><option value='55'>55</option><option value='56'>56</option><option value='57'>57</option><option value='58'>58</option><option value='59'>59</option></select>MIN<p>");
          out.println("<input type='submit' value='Add'></td></tr></table></FORM><p><p>");
          out.println("<table border='1' style='width:300px'><tr><th>FOXIT成效分析管控系統</th></tr>");
          out.println("<tr><td><a href='./SetControl'>成效分析管控系統</a></td></tr>");
          out.println("</table>");
          out.println("</body></html>");
       //} catch (UnknownHostException e) {
       //} catch (MongoException e) {
       } catch (Exception ex) {
          out.println(ex.getMessage());
       }

    }
} 
